package com.scw.util;

	import java.awt.Color;
	import java.awt.Font;
	import java.awt.Graphics;
	import java.awt.image.BufferedImage;
	import java.io.IOException;
	import java.io.OutputStream;
	import java.util.Random;

	import javax.imageio.ImageIO;
	import javax.servlet.ServletException;
	import javax.servlet.http.HttpServlet;
	import javax.servlet.http.HttpServletRequest;
	import javax.servlet.http.HttpServletResponse;
	import javax.servlet.http.HttpSession;


import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

	/**
	 * 
	 * @Description 验证码selvet
	 * @author 王华聪
	 * @version v1.0
	 */
	public class SecurityCode extends HttpServlet{
		private static final long serialVersionUID = 1L;
		private int width = 50;// 宽
		private int height = 20;// 高
		private int num = 4;//验证码位数
		private int joinLetter = 2;//2 加入字母 1 纯数字
		
		public SecurityCode(){
		}
		
		public SecurityCode(int width, int height, int num, int joinLetter) {
			super();
			this.width = width;
			this.height = height;
			this.num = num;
			this.joinLetter = joinLetter;
		}

		@Override
		protected void service(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException {
			this.getSecutityCodeImg(request, response);
		}

		public void getSecutityCodeImg(HttpServletRequest request,HttpServletResponse response) throws IOException {
			HttpSession session = request.getSession();
			OutputStream os = response.getOutputStream();
			// 设置浏览器不要缓存图片
			response.setContentType("image/jpeg");
			response.setHeader("pragma", "No-cache");
			response.setHeader("cache-contor", "no-cacher");
			response.setDateHeader("exists", 0);
			// 创建图片
			BufferedImage image = new BufferedImage(width, height,
					BufferedImage.TYPE_INT_RGB);
			Graphics g = image.getGraphics();
			// 产生随机码
			char[] rands = getCheckCode(num,joinLetter);
			// 产生图像
			g=drawBackgroud(g);
			g=drawRands(g, rands);
			// 结束
			g.dispose();
			session.setAttribute("checkCode", new String(rands).toString());
			// 将图像输出到客户端
			ImageIO.write(image, "JPEG", os);
			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
			encoder.encode(image);
			System.out.println(new String(rands).toString());
		}

		//随机数
		public  char[] getCheckCode(int num,int joinLetter) {
			char[] rands = new char[num];
			Random random = new Random();
			for(int i =0;i<rands.length;i++){
				int j =random.nextInt(joinLetter);
				String charOrNum = j % 2 == 0 ? "char" : "num";
				if("char".equals(charOrNum)){
					int type = random.nextInt(2)% 2 == 0 ? 65:97;
					char ch =(char)(type+random.nextInt(25));
					rands[i] = ch;
				}else{
					rands[i] =(char)(48+random.nextInt(9));
				}
			}
			return rands;
		}
		
		//随机颜色
		public static Color getRandomColor(){
			Random random = new Random();
			return new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255));
		}
		//画背景图
		public Graphics drawBackgroud(Graphics g) {
			g.setColor(Color.YELLOW);
			g.fillRect(0, 0, width, height);//画框大小
			Random random = new Random();
			for(int i=0;i<10;i++){//画干扰点 
				g.setColor(getRandomColor());
				g.drawOval(random.nextInt(width), random.nextInt(height), 1, 0);
			}
			return g;
		}
		//给随机数上色
		public Graphics drawRands(Graphics g, char[] rands) {
			g.setFont(new Font("Times New Roman",Font.PLAIN,12));
			Random random = new Random();
			int w = width/rands.length;
			for(int i=0;i<num;i++){
				g.setColor(getRandomColor());
				
				g.drawString(""+rands[i],w*i+random.nextInt(w-5), 18+random.nextInt(height-18));
			}
			return g;
		}

		public int getWidth() {
			return width;
		}

		public void setWidth(int width) {
			this.width = width;
		}

		public int getHeight() {
			return height;
		}

		public void setHeight(int height) {
			this.height = height;
		}

		public int getNum() {
			return num;
		}

		public void setNum(int num) {
			this.num = num;
		}

		public int getJoinLetter() {
			return joinLetter;
		}

		public void setJoinLetter(int joinLetter) {
			this.joinLetter = joinLetter;
		}
		
	}

